<template>
  <div class="wrap">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      status-icon
      label-width="auto"
      class="ruleForm-wrap"
    >
      <el-form-item label="名称" prop="user_wanted">
        <el-input
          v-model="ruleForm.api_name"
          :disabled="true"
          minlength="5"
          message
          type="text"
        />
      </el-form-item>
      <el-form-item label="类型" prop="need_token">
        <el-radio-group v-model="ruleForm.need_token">
          <el-radio :value="0">无需登录</el-radio>
          <el-radio :value="1">需要登录</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="时间戳" prop="ts_filter">
        <el-radio-group v-model="ruleForm.ts_filter">
          <el-radio :value="0">不校验</el-radio>
          <el-radio :value="1">校验</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="调试日志" prop="log_debug_level">
        <el-checkbox-group v-model="ruleForm.log_debug_level">
          <el-checkbox label="请求" :value="1" />
          <el-checkbox label="响应" :value="2" />
          <el-checkbox label="处理中" :value="3" />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="并发" prop="mobile">
        <el-input
          v-model.number="ruleForm.max_cc"
          autocomplete="off"
          placeholder="请输入并发数"
        />
      </el-form-item>
      <el-form-item class="btn-wrap">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          修改
        </el-button>
        <el-button type="warning" plain @click="resetForm(ruleFormRef)"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
import { ElMessage } from 'element-plus'
import { postData } from '@/http/request.js'
import { useRouter, useRoute } from 'vue-router'
let router = useRouter()
let route = useRoute()
const { proxy } = getCurrentInstance()

let ruleFormRef = ref(null)
const api_name = route.query.api_name

const ruleForm = reactive({
  api_name: api_name,
  need_token: '',
  ts_filter: '',
  log_debug_level: [],
  max_cc: '',
})

const rules = reactive({
  api_name: [{ required: false, message: '', trigger: 'blur' }],
  need_token: [{ required: false, message: '请选择类型', trigger: 'blur' }],
  ts_filter: [
    {
      required: false,
      message: '校验签名时是否校验时间戳',
      trigger: 'blur',
    },
  ],
  log_debug_level: [
    {
      type: 'array',
      required: false,
      message: '请选择API调试日志',
      trigger: 'blur',
    },
  ],
  max_cc: [{ required: false, message: '请输入并发数', trigger: 'blur' }],
})

let getInit = () => {
  postData('/api/get', {
    api_name: api_name,
  })
    .then((res) => {
      var arr = []
      if (res.code == 0) {
        let datas = res.datas
        // Object.assign(ruleForm, datas)
        ruleForm.api_name = datas.api_name
        ruleForm.max_cc = datas.max_cc
        ruleForm.need_token = datas.need_token
        ruleForm.ts_filter = datas.ts_filter
        let log_debug_level_arr = eval(datas.log_debug_level)
        for (var i = 0; i < log_debug_level_arr.length; i++) {
          arr.push(JSON.stringify(log_debug_level_arr[i]))
        }
        //没有生效，好像是接口返回的就一直是空
        //ruleForm.log_debug_level = arr
        //Object.assign(ruleForm.log_debug_level, arr)
      } else if (res.code == -56055) {
        return
      } else {
        ElMessage({
          type: 'error',
          message: res.code + '，' + res.msg,
        })
      }
    })
    .catch((err) => {
      console.log(err)
      ElMessage({
        type: 'error',
        message: '请稍后重试',
      })
    })
}

const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      postData('/sys/admin/update', {
        api_name: ruleForm.api_name,
        need_token: ruleForm.need_token,
        ts_filter: ruleForm.ts_filter,
        log_debug_level: ruleForm.log_debug_level,
        max_cc: ruleForm.max_cc,
      })
        .then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '修改成功',
            })
            setTimeout(() => {
              router.push('/apiList')
            }, 3000)
          } else {
            ElMessage({
              type: 'error',
              message: '修改失败',
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {
  getInit()
})
</script>
<style scope>
.wrap {
  margin: 20px 0 0 20px;
}

.title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  /* background: #c0dee2; */
  padding-left: 30px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  /* border-radius: 12px 12px 0 0; */
  /* font-weight: bold; */
}

.title::before {
  content: '';
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 15px;
}

.title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}

.btn-wrap {
  margin-top: 30px;
}
</style>
